package com.lczyfz.istep.modules.openstack.api.nova.impl.serveraction;


import com.lczyfz.istep.modules.openstack.api.nova.impl.parseJson.NovaParse;
import com.lczyfz.istep.modules.openstack.api.nova.interfaces.serveraction.ConsoleParts;
import com.lczyfz.istep.modules.openstack.api.nova.model.serveraction.ConsoleIndex;
import com.lczyfz.istep.modules.openstack.api.nova.model.serveraction.ConsoleOutput;
import com.lczyfz.istep.modules.openstack.api.nova.model.serveraction.ConsoleOutputLineNum;
import com.lczyfz.istep.modules.openstack.api.nova.model.serveraction.ConsoleType;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONObject;

public class ConsolePartImpl implements ConsoleParts {

	public RequestFactory factory = new RequestFactory("nova");
	
	@Override
	public ConsoleIndex GetConsole(String serverid, ConsoleType consoletype) {

		ConsoleIndex console = new ConsoleIndex();		
		JSONObject params = NovaParse.packageConsoleType(consoletype);
	//	System.out.println(params.toString());
		Request request = factory.getRequest();
		request.setUrl("/servers/"+serverid + "/action");
		request.setRequestType(RequestType.POST);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);
		
		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		console = NovaParse.resolveConsoleType(res);
		return console;
	}

	@Override
	public ConsoleOutput ShowConsoleOutput(String serverid, ConsoleOutputLineNum reqiurelingnum) {

		ConsoleOutput console = new ConsoleOutput();		
		JSONObject params = NovaParse.packageConsoleOutput(reqiurelingnum);
	//	System.out.println(params.toString());
		Request request = factory.getRequest();
		request.setUrl("/servers/"+serverid + "/action");
		request.setRequestType(RequestType.POST);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);
		
		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		console = NovaParse.resolveConsoleOutput(res);
		return console;
	}
	
	public static void main(String[] args){

		ConsolePartImpl showconsoledemo = new ConsolePartImpl();
		System.out.println("��ȡ����̨");
		
		ConsoleType consoletype = new ConsoleType();
		consoletype.setType("novnc");
		ConsoleIndex getconsole = showconsoledemo.GetConsole("6e4eaffa-7198-40f4-9eab-1694cc90237a", consoletype);
		System.out.println(getconsole.getType()+" "+getconsole.getUrl());
		
//		System.out.println("��ʾ����̨���");
//		ConsoleOutputLineNum consolelength = new ConsoleOutputLineNum();
//		consolelength.setLength("50");
//		ConsoleOutput showconsole = showconsoledemo.ShowConsoleOutput("69c30bc5-9d66-4087-b1f2-38b78d9add93", consolelength);
//		System.out.println(showconsole.getOutput());
	}
	
}
